package com.hand.sxt25527.ssmtrain.service.impl;

import com.github.pagehelper.PageHelper;
import com.hand.sxt25527.ssmtrain.dto.ItemEditDto;
import com.hand.sxt25527.ssmtrain.dto.ItemQueryCondition;
import com.hand.sxt25527.ssmtrain.entity.Item;
import com.hand.sxt25527.ssmtrain.mapper.ItemMapper;
import com.hand.sxt25527.ssmtrain.service.IItemServiceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.List;

/**
 * @author Mr~S
 * @date 2019/7/19 11:33
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ItemServiceImpl implements IItemServiceDao {

    @Autowired
    ItemMapper itemMapper;

    @Override
    public List<Item> findAll(Integer page, Integer size) {
        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 5;
        }
        PageHelper.startPage(page, size);

        List<Item> itemsList = null;
        try {
            itemsList = itemMapper.findAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return itemsList;
    }

    @Override
    public int insertItem(Item item) {
        // 查询最大物料编码
        String maxItemCode = itemMapper.findMaxItemCode();
        int maxItemCodeMath = Integer.parseInt(maxItemCode.substring(4)) + 1;
        DecimalFormat format = new DecimalFormat("0000000000");
        item.setItemCode("ITEM" + format.format(maxItemCodeMath));
        return itemMapper.insertItem(item);
    }

    @Override
    public String deleteItem(Long itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return null;
        }
        // 删除之前先去数据库中查询是否存在该对象
        Item item = null;
        try {
            item = itemMapper.selectItemById(itemId);
            //数据库存在后执行删除
            itemMapper.deleteItem(itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return item.toString();

    }

    @Override
    public Item selectItemById(Long itemId) {
        if (StringUtils.isEmpty(itemId)) {
            return null;
        }
        Item item = null;
        try {
            item = itemMapper.selectItemById(itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return item;
    }

    @Override
    public void updateItem(ItemEditDto editDto) {
        itemMapper.updateItem(editDto);
    }

    @Override
    public List<Item> findAllByCondition(Integer page, Integer size, ItemQueryCondition condition) {
        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 5;
        }
        PageHelper.startPage(page, size);

        return itemMapper.findAllByCondition(condition);
    }

    @Override
    public int batchDelItem(List ids) {

        return itemMapper.batchDelItem(ids);
    }
}
